// miniprogram/pages/profile-list/profile-list.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
const MAX_LIMIT = 10
const db = wx.cloud.database()
let type = 0
let setName = ''
var req = require("../../utils/action.js"),utils = require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemList: [],
    pageNo:0,
    pageSize:20,
    tip:'您暂时还没有发布过帖子哦，请赶快去发布吧~',
    type:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    type = options.type
    this.setData({type:type})
    setName = type == 0 ? 'campus-comment' : 'campus'
    if(type == 2){
      this.setData({tip:'您暂时还没有收藏过帖子哦，请赶快去发布吧~'})
      this._getFavorList()
    }else{
      this._getItemList(setName)
    }
    
  },
  onLongPress(e) {
    if(type == 2){
      wx.showModal({
        title: '确认要取消收藏吗？',
        success: function (res) {
          let _url = req.actions.ym+ "invitation/cancelFavorite",that=this
          let params = {invitation_id:e.currentTarget.dataset.item.id,customer_id:getApp().globalData.userInfo.id}
          console.log("paramsparamsparams",params)
          utils.myResquest(params,_url,function(res){
            //console.log("sendComment===>"+JSON.stringify(res))
            wx.hideLoading()
            if(res.data.code == '200'){
              wx.showToast({
                title: '收藏成功~',
                icon: 'none'
              })
              that.setData({"item.favor":true})
            }
          })
        }
      })
    }
    
  },
  _getFavorList(){
    let that = this,_url = req.actions.ym+ "invitation/pageFavorList",
    params={
      customerId:getApp().globalData.userInfo.id,
      pageNo:that.data.pageNo,
      pageSize:that.data.pageSize
    }
    wx.showLoading({title: '加载中',})
    utils.myResquest(params,_url,function(res){
      console.log("pageFavorList:"+JSON.stringify(res))
      that.setData({
        itemList:that.data.itemList.concat(res.data.result.records)
      })
      wx.hideLoading({success: (res) => {}})
    })
  },
  _getItemList() {
    let that = this,_url = req.actions.ym+ "invitation/pageList",
    params={
      customerId:getApp().globalData.userInfo.id,
      pageNo:that.data.pageNo,
      pageSize:that.data.pageSize
    }
    wx.showLoading({title: '加载中'})
    utils.myResquest(params,_url,function(res){
      console.log("_getItemList:"+JSON.stringify(res))
      that.setData({
        itemList:that.data.itemList.concat(res.data.result.records)
      })
      wx.hideLoading({success: (res) => {}})
    })
  },
  goComment(event) {
    console.log(event)
    let itemId = event.currentTarget.dataset.item.id
    wx.navigateTo({
      url: '../../pages/campus-comment/campus-comment?itemId=' + itemId,
    })
  },
})